% By Martin Andreasen
% This function computes the optimal weigthing matrix by a Bartlett kernel
% with max lag q_lag
function Wopt = getOptimalWeighting(qLag,dataSMM)

% We compute the h-function for all observations
T = size(dataSMM.dataMoments_mat,1);
hFunc = dataSMM.dataMoments_mat-repmat(dataSMM.dataMoments,T,1);
S = NeweyWest(hFunc,qLag);

%Wopt = S\eye(size(S,1));
Wopt = diag(diag(S))\eye(size(S,1));

end


